//
//  CRUserData.m
//  ChatRoom
//
//  Created by lee han on 12/7/12.
//  Copyright (c) 2012 lee han. All rights reserved.
//

#import "CRUserData.h"
#import "CRLocalDataMgr.h"
#import "MBProgressHUD.h"
#import "networkDefine.h"

CRUserData* g_userData = nil;

@interface CRUserData(Private)

- (void)loadInfo;

@end

@implementation CRUserData

+ (CRUserData*)sharedInstance
{
    @synchronized(self)
    {
        if (g_userData == nil)
        {
            g_userData = [[CRUserData alloc] init];
        }
    }
    
    return g_userData;
}

- (id)init
{
    if (self = [super init])
    {
        [self loadInfo];
        m_collectionData = [[NSMutableArray alloc] init];
        m_AddressData = [[NSMutableArray alloc] init];
        _mOrderData= [[NSMutableArray alloc] init];
        _mOrderProductData = [[NSMutableDictionary alloc] init];
        m_scollData = [[NSMutableArray alloc] init];
        m_homeData = [[NSMutableArray alloc] init];
        m_expData = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)saveLoginInfo
{
    [[CRLocalDataMgr sharedInstance] savePhoneNumber:self.m_landingPhone];
    [[CRLocalDataMgr sharedInstance] savePwd:self.m_password];
}

#pragma mark private
- (void)loadInfo
{
    self.m_landingPhone = [[CRLocalDataMgr sharedInstance] getPhoneNumber];
    self.m_password = [[CRLocalDataMgr sharedInstance] getPwd];
}
//end private


-(void)setCollectionData:(NSArray*) data{
    [m_collectionData removeAllObjects];
    if([data isEqual:[NSNull null]]){
        return;
    }
    
    for (id info in data){
        [m_collectionData addObject:[info objectForKey:@"pid"]];
    }
}

-(BOOL)hasCollection:(NSString*)productid{
    for (NSString* pid in m_collectionData) {
        if([pid isEqual:productid]){
            return YES;
        }
    }
    return NO;
}

-(void)addCollection:(NSString*)productid{
    [m_collectionData addObject:productid];
}
-(void)removeCollection:(NSString*)productid{
    [m_collectionData removeObject:productid];
}
-(NSInteger)CollectionCount{
    return [m_collectionData count];
}

//购物车
-(void)setShopCarData:(NSArray*)data{
    [m_shopcarData removeAllObjects];
    if([data isEqual:[NSNull null]]){
        return;
    }
    for (id info in data){
        [m_shopcarData setObject:[info objectForKey:@"count"] forKey:[info objectForKey:@"pid"]];
    }
}

-(void)addShopcar:(NSString*)pid count:(NSString*)count{
    NSInteger curcount = [[m_shopcarData objectForKey:pid] integerValue];
    curcount += [count integerValue];
    [m_shopcarData setObject:[NSString stringWithFormat:@"%ld", curcount] forKey:pid];
}

-(void)removeShopcar:(NSString*)sid{
    [m_shopcarData removeObjectForKey:sid];
}


-(void)setAddressData:(NSArray*)data{
    [m_AddressData removeAllObjects];
    if([data isEqual:[NSNull null]]){
        return;
    }
    for (NSDictionary* adata in data) {
        [m_AddressData addObject:[NSMutableDictionary dictionaryWithDictionary:adata]];
    }
}

-(NSDictionary*)getAddressData:(NSInteger)index{
    return [m_AddressData objectAtIndex:index];
}

-(void)delAddressData:(NSInteger)aid{
    for (NSDictionary* data in m_AddressData) {
        if([[data objectForKey:@"id"] integerValue] == aid){
            [m_AddressData removeObject:data];
            return;
        }
    }
}

-(void)setAddressSelect:(NSInteger)aid old:(NSInteger)oldAid{
    NSInteger tempaid;
    for (NSMutableDictionary* data in m_AddressData) {
        tempaid = [[data objectForKey:@"id"] integerValue];
        if(tempaid == aid){
            [data setObject:@"1" forKey:@"selected"];
        }
        else if (tempaid == oldAid){
            [data setObject:@"0" forKey:@"selected"];
        }
    }
}

-(NSInteger)getDefaultAddressID{
    for (NSMutableDictionary* data in m_AddressData) {
        if([[data objectForKey:@"selected"] integerValue] == 1){
            return [[data objectForKey:@"id"] integerValue];
        }
    }
    return 0;
}

-(NSInteger)getAddressCount{
    if(m_AddressData == NULL){
        return 0;
    }
    return [m_AddressData count];
}

-(NSDictionary*)getOrderAddress{
    for (NSDictionary* info in m_AddressData) {
        if([[info objectForKey:@"id"] integerValue] == _mOrderAddressID){
            return info;
        }
    }
    return nil;
}

-(NSInteger)getOrderMoney{
    NSInteger ret = 0;
    NSInteger count, price;
    for (NSDictionary* info in _mOrderData) {
        count = [[info objectForKey:@"count"] integerValue];
        price = [[info objectForKey:@"price"] integerValue];
        ret += (count*price);
    }
    return ret;
}

#pragma mark 首页数据
-(void)setHomeData:(NSDictionary*) data {
    [self setScrollData:[data objectForKey:@"scroll"]];
    [self setHomeCateData:[data objectForKey:@"home"]];
    [self setExpData:[data objectForKey:@"exp"]];
}

-(NSArray*)getHomeData {
    return m_homeData;
}

-(void)setExpData:(NSArray*)data {
    [m_expData removeAllObjects];
    if([data isEqual:[NSNull null]]){
        return;
    }
    
    for (id info in data){
        [m_expData addObject:info];
    }
}
- (NSArray*)getExpData {
    return m_expData;
}

-(void)setHomeCateData:(NSArray*) data {
    [m_homeData removeAllObjects];
    if([data isEqual:[NSNull null]]){
        return;
    }
    
    for (id info in data){
        [m_homeData addObject:info];
    }
}

-(void)setScrollData:(NSArray*) data {
    [m_scollData removeAllObjects];
    if([data isEqual:[NSNull null]]){
        return;
    }
    
    for (id info in data){
        [m_scollData addObject:info];
    }
}

-(NSArray*)getScrollImage {
    NSMutableArray* array = [[NSMutableArray alloc] init];
    for (id info in m_scollData) {
        NSString* urlstr = [URLIMAGE_PRE stringByAppendingString:[info objectForKey:@"path"]];
        [array addObject:urlstr];
    }
    return array;
}

-(NSArray*)getScrollTitle {
    NSMutableArray* array = [[NSMutableArray alloc] init];
    for (id info in m_scollData) {
        [array addObject:[info objectForKey:@"name"]];
    }
    return array;
}

-(NSDictionary*)getScrollInfo:(NSInteger)index {
    return [m_scollData objectAtIndex:index];
}
//end 首页
@end
